<?php

/**
 * This is the model class for table "posts".
 * @property int $post_id
 * @property int $user_id 
 * @property int $category_id
 * @property string $title
 * @property string $content
 * @property date $time_created
 * @property int $total_like
 * @property int $total_share
 * @property string $attachment
 * @property string $venue_name_event
 * @property string $street_address_event
 * @property date $time_start_event
 * @property date $time_end_event
 * @property string $organizer_name
 * @property string $organizer_description_event
 * @property int $status
 * @property string $web_addr
 */
class Posts extends CActiveRecord {

    const IS_PRIVATE = 1;
    const IS_PUBLIC = 0;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public static function model($className = __CLASS__) {
	return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
	return '{{posts}}';
    }

    public function relations() {
	return array(
	    'user' => array(self::BELONGS_TO, 'User', 'user_id'),
	    'cat' => array(self::BELONGS_TO, 'Category', 'category_id'),
	    'comment' => array(self::HAS_MANY, 'Comment', 'post_id'),
	    'abc' => array(self::HAS_MANY, 'Category', 'category_id')
	);
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
	return array(
	    array('title, content, is_private', 'required'),
	    array('post_id, category_id, venue_name_event, street_address_event, web_addr, total_like, time_created, total_share, attachment, organizer_name, organizer_description_event', 'safe')
	);
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
	return array(
	    'title' => 'Title',
	    'content' => 'Content',
	    'is_private' => 'Public/Private'
//'venue_name_event' => 'Venue Name',
//'street_address_event' => 'Street Address',
	);
    }

    /**
     * post a newsfeed based on category     
     */
    public function postNewsFeed($iCat, $user_id, $attachment, $title, $content
    , $venue_name_event, $street_address_event, $time_start_event, $time_end_event, $organizer_name, $organizer_description, $status, $web_addr) {

	$this->user_id = $user_id;
	$this->category_id = $iCat;
	$this->attachment = $attachment;
	$this->title = $title;
	$this->content = $content;
	$this->venue_name_event = $venue_name_event;
    }

    /**
     * After delete post
     */
    public function afterDelete() {
	parent::afterDelete();

	////delete img
	$aAttachment = unserialize($this->attachment);
	if (isset($aAttachment['photo']) && $aAttachment['photo']) {
	    //delete old file
	    foreach ($aAttachment['photo'] as $img) {
		if ($aAttachment['photo'] && file_exists(UPLOAD_PATH_POST . $img)) {
		    unlink(UPLOAD_PATH_POST . $img);
		}
	    }
	}
	////delete comment
	db()->createCommand('DELETE FROM {{comments}} WHERE post_id = ' . $this->post_id);

	////delete like
	db()->createCommand('DELETE FROM {{likes}} WHERE post_id = ' . $this->post_id . ' and type_id = "post"');
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
	$criteria = new CDbCriteria;

	$criteria->compare('post_id', $this->post_id, true);
	$criteria->compare('title', $this->title, true);
	$criteria->compare('category_id', $this->category_id, true);
//order
	$criteria->order = 'post_id DESC';

	return new CActiveDataProvider($this, array(
	    'criteria' => $criteria,
	    'pagination' => array(
		'pageSize' => param('pageSize')
	    )
	));
    }

}

